<?php
namespace app\admin\controller;
use app\admin\model\MySubject;
use builder\Builder;
use think\Db;
use think\Request;

class Document
{
    public function show_list(Request $request){
        $cid = $request->param('cid');
        $obj = new MySubject();
        $obj->setCid($cid);
        $model = $obj->getModel();
        $field = $obj->getField();
        $builder = Builder::table()
            ->setAction('document/page?cid='.$cid)
            ->setCid($cid)
            ->setMonitorAction('switch','document/change')
            ->setMonitorAction('edit','document/change')
            ->setRightHeader();
//        echo '<pre>';
//        print_r($model['right']);
//        exit;
        foreach ($model['toops'] as $value){
            if($value=='alldel'){
                $builder = $builder->addToolbarDemo('alldel','全部删除',[],'document/del');
            }
            if($value=='add'){
                $builder = $builder->addToolbarDemo('add','添加',[],'document/show_add');
            }
            if($value=='back'){
                $builder = $builder->addToolbarDemo('back','返回');
            }
            if($value=='text'){
                $builder = $builder->addToolbarDemo('text',$model['name']);
            }
            if($value=='search'){
                $newfield = [];
                foreach ($field as $value){
                    if($value['search']==1){
                        $newfield[] = $value;
                    }
                }
                $builder = $builder->addToolbarDemo('search','搜索',$newfield);
            }
        }
        foreach ($field as $value){
            if($value['show']==1){
                if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='联动下拉框'||$value['form_type']=='编辑框'||$value['form_type']=='单选框'||$value['form_type']=='多选框'||$value['form_type']=='联动单选框'||$value['form_type']=='联动多选框'||$value['form_type']=='联动下拉组'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name'],'document/change');
                }
                if($value['form_type']=='时间'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='单图片'){
                    $builder = $builder->setColumn('img',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='多图片'){
                    $builder = $builder->setColumn('imgs',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='开关'){
                    if($value['option']){
                        $array = explode(',',$value['option']);
                    }else{
                        $array = ['开启','关闭'];
                    }
                    $builder = $builder->setColumn('switch',$value['field'],$value['field_name'],'document/change',null,'center','',$array);
                }
            }
        }
        foreach ($model['right'] as $value){
            if($value=='edit'){
                $builder->setButton($value,'编辑','document/show_edit');
            }
            if($value=='del'){
                $builder->setButton($value,'删除','document/del');
            }
            if($value=='add'){
                $builder->setButton($value,'添加','document/show_add');
            }
            if($value=='detail'){
                $builder->setButton($value,'查看','document/detail');
            }
            if($value=='a'){
                $builder->setButton($value,'编辑字段','document/show_list');
            }
        }
        return $builder->setPage()
            ->fetch();
    }
    public function page(Request $request){
        $cid = $request->param('cid');
        $limit = $request->param('limit',10);
        $fields = $request->param('field',null);
        $tiaojian = $request->param('tiaojian',null);
        $obj = new MySubject();
        $obj->setCid($cid);
        $table = $obj->getModel('table');
        $field = $obj->getField();
        $data = Db::name($table)
            ->where('delete_time',null);
        if($tiaojian){
            if($fields){
                foreach ($field as $value){
                    if($value['field']==$fields){
                        if($fields&&$tiaojian){
                            if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'){
                                $data = $data->where($fields,'like',"%$tiaojian%");
                            }
                            if($value['form_type']=='开关'){
                                if($tiaojian=='开启'){
                                    $tiaojian = 1;
                                }else{
                                    $tiaojian = 0;
                                }
                                $data = $data->where($fields,$tiaojian);
                            }
                            if($value['form_type']=='联动下拉框'||$value['form_type']=='联动单选框'){
                                $table = Db::name(getTable('models'))
                                    ->where('id',$value['table'])
                                    ->find();
                                $mydas = Db::name($table['table'])
                                    ->where('name','like',"%$tiaojian%")
                                    ->select();
                                $mydasid = [];
                                foreach ($mydas as $vvv){
                                    $mydasid[] = $vvv['id'];
                                }
                                $data = $data->where($fields,'in',$mydasid);
                            }
                            if($value['form_type']=='联动下拉组'){
                                $table = explode(',',$value['table']);
                                $table = Db::name(getTable('models'))
                                    ->where('id',$table[1])
                                    ->find();
                                $mydas = Db::name($table['table'])
                                    ->where('name','like',"%$tiaojian%")
                                    ->select();
                                $mydasid = [];
                                foreach ($mydas as $vvv){
                                    $mydasid[] = $vvv['id'];
                                }
                                $data = $data->where($fields,'in',$mydasid);
                            }
                        }
                    }
                }
            }else{
                $data = $data->where('id|name|dir','like',"%$tiaojian%");
            }
        }
        $data = $data->where('cid',$cid)
            ->paginate($limit)
            ->toArray();
//        echo '<pre>';
//        print_r($data);
//        exit;
        foreach ($data['data'] as $key=>$value){
            foreach ($field as $va){
                if($va['form_type']=='时间'){
                    $value[$va['field']] = date('Y-m-d',$value[$va['field']]);
                }
                if($va['form_type']=='单图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']]);
                }
                if($va['form_type']=='多图片'){
                    $value[$va['field']] = getFilePath($value[$va['field']],true);
                }
//                if($va['form_type']=='单选框'){
//                    $value[$va['field']] = getFilePath($value[$va['field']],false);
//                }
                if($va['form_type']=='联动下拉框'||$va['form_type']=='联动单选框'){
                    if(!$value[$va['field']]){
                        $value[$va['field']] = $va['defaults'];
                        continue;
                    }
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $shu = Db::name($table['table'])
                        ->where('id',$value[$va['field']])
                        ->find();
                    $value[$va['field']] = $shu['name'];
                }
                if($va['form_type']=='联动多选框'){
                    $table = Db::name(getTable('models'))
                        ->where('id',$va['table'])
                        ->find();
                    $shu = Db::name($table['table'])
                        ->where('id','in',$value[$va['field']])
                        ->select();
                    $str = '';
                    foreach ($shu as $vs){
                        $str.=','.$vs['name'];
                    }
                    $str = trim($str,',');
                    $value[$va['field']] = $str;
                }
                if($va['form_type']=='联动下拉组'){
                    $table1 = explode(',',$va['table']);
                    $table1 = Db::name(getTable('models'))->where('id',$table1[1])->find();
                    $mydata = Db::name($table1['table'])->where('id',$value[$va['field']])->where('status',1)->field('name')->find();
                    if($mydata){
                        $value[$va['field']] = $mydata['name'];
                    }else{
                        $value[$va['field']] = '';
                    }
                }
            }
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }
    public function show_add(Request $request){
        $cid = $request->param('cid');
        $obj = new MySubject();
        $obj->setCid($cid);
        $data['update_time'] = time();
        return $obj->getFormToCid('document/add',null,true,[],$cid);
    }
    public function add(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setCid($data['cid']);
        $data = $obj->getData($data);
        $table = $obj->getModel('table');
        if(Db::name($table)->insert($data,true)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function show_edit(Request $request){
        $cid = $request->param('cid');
        $id = $request->param('id');
        $obj = new MySubject();
        $obj->setCid($cid);
        return $obj->getFormToCid('document/edit',$id,true,[],$cid);
    }
    public function edit(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setCid($data['cid']);
        $data = $obj->getData($data);
        $table = $obj->getModel('table');
        $data['update_time'] = time();
        if(Db::name($table)->where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function change(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setCid($data['cid']);
        $table = $obj->getModel('table');
        $datas= Db::name($table)
            ->where('id',$data['id'])
            ->find();
        if($datas){
            if($datas[$data['field']]==1){
                $datas[$data['field']] = 0;
            }else{
                $datas[$data['field']] = 1;
            }
        }
        if(Db::name($table)->where('id',$datas['id'])->update($datas)){
            return json_to_respos('修改成功');
        }else{
            return json_to_respos('修改失败',205);
        }
    }
    public function detail(Request $request){
        $cid = $request->param('cid');
        $id = $request->param('id');
        $obj = new MySubject();
        $obj->setCid($cid);
        return $obj->getFormToCid('document/edit',$id,false,[],$cid);
    }
    public function del(Request $request){
//        $data = $request->param();
        $cid = $request->param('cid');
        $id = $request->param('id');
        $obj = new MySubject();
        $obj->setCid($cid);
        $table = $obj->getModel('table');
        $data = Db::name($table)->where('id','in',$id)->select();
        foreach ($data as $value){
            if($value['delete']!==1){
                return json_to_respos('有不可删除数据',205);
            }
        }
        if(Db::name($table)->where('id','in',$id)->update(['delete_time'=>1])){
            return json_to_respos('删除成功');
        }else{
            return json_to_respos('删除失败',205);
        }
    }
}